#include <map>

#include "raster.hpp"
#include "terrain_improvement.hpp"
#include "wml_node.hpp"
#include "wml_utils.hpp"

namespace {
typedef std::map<std::string, terrain_improvement_ptr> cache_map;
cache_map cache;
}

void terrain_improvement::init(wml::const_node_ptr node)
{
	wml::node::const_child_iterator i1 = node->begin_child("improvement");
	wml::node::const_child_iterator i2 = node->end_child("improvement");
	for(; i1 != i2; ++i1) {
		terrain_improvement_ptr t(new terrain_improvement(i1->second));
		cache[t->id()] = t;
	}
}

const_terrain_improvement_ptr terrain_improvement::get(const std::string& key)
{
	cache_map::const_iterator i = cache.find(key);
	if(i != cache.end()) {
		return i->second;
	} else {
		return const_terrain_improvement_ptr();
	}
}

terrain_improvement::terrain_improvement(wml::const_node_ptr node)
  : id_(node->attr("id")), texture_(graphics::texture::get(node->attr("image"))), cost_(wml::get_int(node, "cost"))
{}

void terrain_improvement::draw(int x, int y) const
{
	graphics::blit_texture(texture_, x, y);
}
